<?php

/**
 *
 * @author Kevin_ren  <330202207@qq.com>
 */

namespace Common\Model;

use Think\Model;

use Think\Model\RelationModel;

class CommonModel extends RelationModel
{

    protected $pk        = '';              //主键名
    protected $tableName =  '';             //表名
    protected $token     = '';
    protected $cacheTime = 86400;           //缓存时间
    protected $orderby   = array();         //针对全部查询出的数据的排序

    public function updateCount($id, $col, $num = 1)
    {
        $id = (int)$id;
        return $this->execute(" update ".$this->getTableName()." set {$col} = ({$col} + '{$num}') where ".$this->pk." = '{$id}' ");
    }

    public function itemsByIds($ids = array())
    {
        if(empty($ids)) return array();
        $data = $this->where(array($this->pk => array('IN', $ids)))->select();
        $return = array();
        foreach ($data as $val) {
            $return[$val[$this->pk]] = $val;
        }
        return $return;
    }
   
    public function fetchAll()
    {
        $cache = S(array('type' => 'File', 'expire' => $this->cacheTime));
        if (!$data = $cache->get($this->token)) {
            $result = $this->order($this->orderby)->select();
            $data = array();
            foreach ($result  as $row) {
                $data[$row[$this->pk]] = $this->_format($row);
            }
            $cache->set($this->token,$data);
        }   
        return $data;
     }
     
    public function cleanCache()
    {
        $cache = S(array('type' => 'File', 'expire' => $this->cacheTime));
        $cache->rm($this->token);
    }

    public function _format($data)
    {
        return $data;
    }


    /**
     * 添加/编辑
     * @author Kevin_ren  <330202207@qq.com>
     */
    public function saveData()
    {
        $data = I('post.', false);

        if ($this->create($data)) {

            $data['update_time'] = NOW_TIME;

            if ($data['id']) {
                $result = $this->save($data);
            } else {
                $data['create_time'] = NOW_TIME;
                $result = $this->add($data);
            }

            if ($result || $result !== false) {
                return true;
            } else {
                return false;
            }

        } else {
            return false;
        }
    }

    /**
     * 获取所有数据的公共方法
     * @author Kevin_ren  <330202207@qq.com>
     */
    public function getAllData($where = array(), $fields = '', $order = '')
    {
        $pk     =  $this->pk;
        $order  =  empty($order)  ? $pk .' desc' : $order;
        $fields =  empty($fields) ? true :$fields;

        $map = array('is_del' => array('eq', 0));

        $options['where'] = array_filter(array_merge($map, $where), function ($val) {
            if ($val === '' || $val === null) {
                return false;
            } else {
                return true;
            }
        });

        if (empty($where)) {
            $options['where'] =  $map;
        }

        $data   =  $this->where($options['where'])->field($fields)->order($order)->select();
        return  $this->deliveryData($data);
    }

    /**
     * 过滤数据
     * @author Kevin_ren  <330202207@qq.com>
     */
    protected function deliveryData($data)
    {
        return empty($data) ? false : $data;
    }

    public function findData($id)
    {
        return $this->find($id);
    }

}